const GetPeriod = require("../../utils/getperiod.js");
const app =getApp()
Page({
  data: {
    dateType: 0,
    dateTypeR: 'now',
    startDate:'',
    endDate:''
  },
  onLoad() {
    this.time = new GetPeriod();
    this.setData({ dateType: 0, startDate: this.time.getNowDate(), endDate: this.time.getNowDate(), date: this.time.getPeriod() });
  },
  // 获取时段字段
  getDateTypeR(e) {
    let dateType = e.currentTarget.dataset.datetype;
    this.setData({ 
      dateTypeR: dateType, 
      date: this.time.getPeriod({ periodType: dateType }) })
  },
  // 获取当前时间段
  getDateType(e) {
    let dateType = e.currentTarget.dataset.datetype;
    let startDate = '', endDate = '';
    var point = this;
    if (dateType == 0) {//今天
      startDate = this.time.getNowDate();
      endDate = this.time.getNowDate();
      this.getOrder(startDate,endDate,point)
      console.log(startDate + "~" + endDate)
    } else if (dateType == 1) {//本周
      startDate = this.time.getWeekStartDate();
      endDate = this.time.getWeekEndDate();
      this.getOrder(startDate, endDate, point)
      console.log(startDate + "~" + endDate)
    } else if (dateType == 2) {//本月
      startDate = this.time.getMonthStartDate();
      endDate = this.time.getMonthEndDate();
      this.getOrder(startDate, endDate, point)
      console.log(startDate + "~" + endDate)
    } else if (dateType == 3) {//本年
      startDate = this.time.getYearStartDate();
      endDate = this.time.getYearEndDate();
      this.getOrder(startDate, endDate, point)
      console.log(startDate + "~" + endDate)
    } else if (dateType == 4) {//选择时段初始化为当天时间段
      startDate = this.time.getNowDate();
      endDate = this.time.getNowDate();
      this.getOrder(startDate, endDate, point)
      console.log(startDate + "~" + endDate)
    } else if (dateType == 5) {//本季度
      startDate = this.time.getQuarterStartDate();
      endDate = this.time.getQuarterEndDate();
      this.getOrder(startDate, endDate, point)
      console.log(startDate + "~" + endDate)
    }
    this.setData({ dateType: dateType, startDate: startDate, endDate: endDate });
  },
  bindDateChange(e) {
    var point = this
    if (e.currentTarget.id == 'start') {
      this.setData({ startDate: e.detail.value })
      this.getOrder(this.data.startDate,this.data.endDate,point)
    } else if (e.currentTarget.id == 'end') {
      this.setData({ endDate: e.detail.value })
      this.getOrder(this.data.startDate, this.data.endDate, point)
    } else {
      return;
    }
  },
  getOrder: function (start_date, end_date, point) {
    var token = app.globalData.token
    var that = this
    var ids = new Array()
    console.log("our token is:" + token)
    wx.showLoading({
      title: '加载中....',
    })
    wx.request({
      url: 'https://www.cycs023.cn/f50m-web/api/orders?',
      data: {
        createStartTime: start_date + " 00:00:00",
        createEndTime: end_date + " 23:59:59",
        status: app.globalData.peisongCompleted
      },
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'X-SDX-Token': token
      },
      success: function (res) {
        if (res.statusCode == 200) {
          console.log("获取到的为：" + res)
          point.setData({
            orderInfo: res.data.data.rows,
            counter: res.data.data.total,

          })
          var newTime = new Array()
          var newDate = new Array()
          for (var i = 0; i < res.data.data.rows.length; i++) {
            newDate = res.data.data.rows[i].updateTime
            let time = 'orderInfo[' + i + '].receiptTime'
            that.setData({
              [time]: res.data.data.rows[i].receiptTime.substring(11, 16)
            })
            console.log(that.data.orderInfo[i].updateTime)
            let date = 'orderInfo[' + i + '].apartmentId'
            console.log(newDate.substring(5, 10))
            that.setData({
              [date]: newDate.substring(5, 10)
            })
          }
          console.log(point.data.orderInfo)
          wx.hideLoading();
        } else if (res.statusCode == 401) {
          wx.hideLoading();
        }
      }
    })
  },
})